<template>
  <div class="register">
    <div class="register-container">
      <div class="register-header">
        <h2>注册</h2>
        <p>创建您的账户</p>
      </div>
      
      <div class="register-form">
        <div class="form-item">
          <input 
            type="text" 
            v-model="form.username" 
            placeholder="请输入用户名"
            class="form-input"
          />
        </div>
        
        <div class="form-item">
          <input 
            type="email" 
            v-model="form.email" 
            placeholder="请输入邮箱"
            class="form-input"
          />
        </div>
        
        <div class="form-item">
          <input 
            type="password" 
            v-model="form.password" 
            placeholder="请输入密码"
            class="form-input"
          />
        </div>
        
        <div class="form-item">
          <input 
            type="password" 
            v-model="form.confirmPassword" 
            placeholder="请确认密码"
            class="form-input"
          />
        </div>
        
        <div class="form-item">
          <button 
            @click="handleRegister" 
            :disabled="loading"
            class="btn btn-primary register-btn"
          >
            {{ loading ? '注册中...' : '注册' }}
          </button>
        </div>
        
        <div class="form-footer">
          <span @click="goToLogin" class="login-link">已有账号？立即登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const form = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const handleRegister = async () => {
  if (!form.username || !form.email || !form.password || !form.confirmPassword) {
    alert('请填写完整信息')
    return
  }
  
  if (form.password !== form.confirmPassword) {
    alert('两次输入的密码不一致')
    return
  }
  
  if (form.password.length < 6) {
    alert('密码长度至少6位')
    return
  }
  
  loading.value = true
  try {
    const result = await userStore.register(form)
    
    if (result.success) {
      alert(result.message)
      router.push('/login')
    } else {
      alert(result.message)
    }
  } catch (error) {
    console.error('注册失败:', error)
    alert('注册失败，请重试')
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-container {
  background-color: #fff;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.register-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 8px;
}

.register-header p {
  color: #666;
  font-size: 14px;
}

.register-form {
  width: 100%;
}

.form-item {
  margin-bottom: 20px;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
}

.form-input:focus {
  border-color: #007aff;
}

.register-btn {
  width: 100%;
  padding: 14px;
  font-size: 16px;
  font-weight: bold;
}

.register-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
}

.login-link {
  color: #007aff;
  font-size: 14px;
  cursor: pointer;
}

.login-link:hover {
  text-decoration: underline;
}
</style> 